/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.controller.testbed;

import org.testis.gui.mvc.controller.AbstractController;
import org.testis.gui.mvc.controller.TestBedControllerList;
import org.testis.gui.mvc.model.testbed.NetworkSerialConfigModel;
import org.testis.gui.mvc.view.testbed.NetworkSerialConfigView;
import org.testis.orm.testbed.datahandlers.NetworkSerialConfigDataHandler;
import org.testis.orm.testbed.refined.NetworkSerialConfigFactory;
import org.testis.orm.testbed.refined.NetworkSerialConfigRefined;
import org.testis.utils.tree.TbedMutableTreeNode;

/**
 *
 * @author selcuk
 */
public class NetworkSerialConfigController extends AbstractController {

    public static final String VISIBLE_PROPERTY = "Visibility";
    public static final String ERROR_PROPERTY = "LastError";
    public static final String NAME_PROPERTY = "Name";
    public static final String ID_PROPERTY = "Id";
    public static final String IP_PROPERTY = "Ip";
    public static final String PORT_PROPERTY = "Port";
    private TbedMutableTreeNode m_selectedNode = null;

    public NetworkSerialConfigController() {
        super();
    }

    private void initialize(boolean editMode) {
        NetworkSerialConfigModel model = new NetworkSerialConfigModel();
        NetworkSerialConfigView view = new NetworkSerialConfigView(
                TestBedControllerList.newInstance().getParentFrame(),
                true, editMode);
        super.addModel(model);
        super.addView(view);
        view.pack();
    }

    public void operationCloseWindow() {
        super.cleanAll();
    }

    public void operationEditConfig(TbedMutableTreeNode node) {
        if (node.getUserObject() instanceof NetworkSerialConfigRefined) {
            m_selectedNode = node;
            NetworkSerialConfigRefined refined = getSelectedConfig();
            if (refined == null) {
                System.out.println(
                        "Not found design part for NetworkSerialConfigRefined");
                return;
            }
            initialize(true);
            setModelProperty(ID_PROPERTY, refined.getId());
            setModelProperty(NAME_PROPERTY, refined.getName());
            setModelProperty(IP_PROPERTY, refined.getIp());
            setModelProperty(PORT_PROPERTY, refined.getPort());
            setModelProperty(VISIBLE_PROPERTY, true);
        }
    }

    public void operationNewConfig() {
        initialize(false);
        setModelProperty(VISIBLE_PROPERTY, true);
    }

    public void operationSaveConfig(int id, String name, String ip, String port) {
        boolean result = NetworkSerialConfigDataHandler.newInstance().saveConfig(
                id, name, ip, port);
        if (!result) {
            setModelProperty(ERROR_PROPERTY,
                    "Unable to edit NetworkSerialConfig record");
        }
    }

    public void operationAddConfig(String name, String ip, String port) {
        NetworkSerialConfigRefined refined = NetworkSerialConfigFactory.newInstance().create(
                name, ip, port);
        if (refined != null) {
            TestBedControllerList.newInstance().getTbedTreeController().operationAddTestBedItem(
                    refined);
        } else {
            setModelProperty(ERROR_PROPERTY,
                    "Unable to add NetworkSerialConfig record");
        }
    }

    private NetworkSerialConfigRefined getSelectedConfig() {
        NetworkSerialConfigRefined result = null;
        Object obj = m_selectedNode.getUserObject();
        if (obj != null && obj instanceof NetworkSerialConfigRefined) {
            result = (NetworkSerialConfigRefined) obj;
        }
        return result;
    }
}
